package com.shijie.controller.erp;

import com.shijie.service.AuthorsService;
import com.shijie.domain.entity.Authors;
import com.shijie.domain.entity.Brands;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;

import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/authors")
public class AuthorsController {

    @Resource
	private AuthorsService authorsService;

	@GetMapping
    public Result search(@RequestParam(required=false) String q) {		
		Condition condition = new Condition(Authors.class);
		if(!StringUtils.isEmpty(q)){
			condition.createCriteria().andLike("name", "%"+q+"%");
		}
        List<Authors> list = authorsService.selectByCondition(condition);
        return ResultGenerator.success(list);
	}

	@RequiresPermissions(value={"users:author:list"})
	@GetMapping("list")
	public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
			@RequestParam(required = false) String q) {
		PageHelper.startPage(page, pageSize);
		Condition condition = new Condition(Authors.class);
		if(!StringUtils.isEmpty(q)){
			condition.createCriteria().andLike("name", "%"+q+"%");
		}
		List<Authors> list = authorsService.selectByCondition(condition);
		PageInfo<Authors> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}
	
	@PostMapping
	@RequiresPermissions(value={"users:author:create","users:author:edit"})
	@Log(title = "作者管理", action = BusinessType.INSERT)
    public Result add(@RequestBody Authors authors) {
        authorsService.save(authors);
        return ResultGenerator.success();
	}

	@RequiresPermissions(value={"users:author:delete"})
	@Log(title = "作者管理", action = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        authorsService.deleteById(id);
        return ResultGenerator.success();
	}

}

